-- AppsFlyerAgent
-- Created by chengb Jun/14/2016
-- AppsFlyer数据统计代理

module("AppsFlyerAgent", package.seeall);

require "game/sdk/AFConst"

-- 关注的炼金坊强化项
local CARE_WORKSHOP_ITEMS = { 1, 2 };

-- 新手关迷宫编号
local NEWBIE_DUNGEON_ID = 11;

-- 舰长冈布奥编号
local CAPTAIN_PET_ID = 57;

-- 月卡商品编号
local SUPERIOR_GOODS_ID = 5001;

-- 内部函数声明
local getUserKey;
local trackEvent;
local whenAutoPatchEnterScene;
local whenAutoPatchShowLogo;
local whenAutoPatchStart;
local whenAutoPatchResult;
local whenAutoPatchFinish;
local whenAutoPatchGotoMain;
local whenCreateNewChar;
local whenTryGuestLogin;
local whenLoginStageChange;
local whenWorkshopStrengthen;
local whenWorkshopBuyEquip;
local whenStartDungeon;
local whenNextFloor;
local whenDungeonPass;
local whenGainPet;
local whenSkyRepair;
local whenBuyGoods;
local whenFBShareSucc;
local whenLineShareSucc;
local whenClickForum;
local whenClickFacebook;
local whenPkgDownloadBegin;
local whenPkgDownloadEnd;

-- 初始化
function init()
    EventMgr.removeAll("AppsFlyerAgent");

    -- 关注进入自动更新场景的事件
    EventMgr.register("AppsFlyerAgent", event.AUTOPATCH_ENTER_SCENE, whenAutoPatchEnterScene);

    -- 关注自动更新显示LOGO的事件
    EventMgr.register("AppsFlyerAgent", event.AUTOPATCH_SHOW_LOGO, whenAutoPatchShowLogo);

    -- 关注自动更新开始的事件
    EventMgr.register("AppsFlyerAgent", event.AUTOPATCH_START, whenAutoPatchStart);

    -- 关注自动更新结果的事件
    EventMgr.register("AppsFlyerAgent", event.AUTOPATCH_RESULT, whenAutoPatchResult);

    -- 关注自动更新完成的事件
    EventMgr.register("AppsFlyerAgent", event.AUTOPATCH_FINISH, whenAutoPatchFinish);

    -- 关注自动更新进入主场景
    EventMgr.register("AppsFlyerAgent", event.AUTOPATCH_GOTO_MAIN, whenAutoPatchGotoMain);

    -- 关注SDK账号登录成功的事件
    EventMgr.register("AppsFlyerAgent", event.GOOGLE_CONNECTED, whenThirdAccountLogined);
    EventMgr.register("AppsFlyerAgent", event.FACEBOOK_CONNECTED, whenThirdAccountLogined);
    EventMgr.register("AppsFlyerAgent", event.GAME_CENTER_CONNECTED, whenThirdAccountLogined);
    EventMgr.register("AppsFlyerAgent", event.THIRD_ACCOUNT_LOGINED, whenThirdAccountLogined);
    EventMgr.register("AppsFlyerAgent", event.TRY_GUEST_LOGIN, whenTryGuestLogin);

    -- 关注登录阶段变化的事件
    EventMgr.register("AppsFlyerAgent", event.LOGIN_STAGE_CHANGE, whenLoginStageChange);

    -- 关注创建角色的事件
    EventMgr.register("AppsFlyerAgent", event.CREATE_NEW_CHAR, whenCreateNewChar);

    -- 关注炼金坊建筑强化事件
    EventMgr.register("AppsFlyerAgent", event.WORKSHOP_STRENGTHEN, whenWorkshopStrengthen);

    -- 关注炼金坊购买装备的事件
    EventMgr.register("AppsFlyerAgent", event.WORKSHOP_BUY_EQUIP, whenWorkshopBuyEquip);

    -- 关注玩家开始迷宫的事件
    EventMgr.register("AppsFlyerAgent", event.START_DUNGEON, whenStartDungeon);

    -- 关注玩家进入下一层的事件
    EventMgr.register("AppsFlyerAgent", event.NEXT_FLOOR, whenNextFloor);

    -- 关注通关迷宫的事件
    EventMgr.register("AppsFlyerAgent", event.DUNGEON_PASS, whenDungeonPass);

    -- 关注获得冈布奥的事件
    EventMgr.register("AppsFlyerAgent", event.GAIN_PET, whenGainPet);

    -- 关注修理天空之城飞艇的事件
    EventMgr.register("AppsFlyerAgent", event.SKY_CITY_REPAIR, whenSkyRepair);

    -- 关注商城购买的事件
    EventMgr.register("AppsFlyerAgent", event.BUY_GOODS, whenBuyGoods);

    -- 关注facebook分享成功的事件
    EventMgr.register("AppsFlyerAgent", event.FB_SHARE_SUCC, whenFBShareSucc);

    -- 关注line分享成功的事件
    EventMgr.register("AppsFlyerAgent", event.LINE_SHARE_SUCC, whenLineShareSucc);

    -- 关注点击论坛的事件
    EventMgr.register("AppsFlyerAgent", event.CLICK_FORUM, whenClickForum);

    -- 关注点击facebook的事件
    EventMgr.register("AppsFlyerAgent", event.CLICK_FACEBOOK, whenClickFacebook);

    -- 关注开始分包下载事件
    EventMgr.register("AppsFlyerAgent", event.BEGIN_PKG_DOWNLOAD, whenPkgDownloadBegin);

    -- 关注完成分包下载事件
    EventMgr.register("AppsFlyerAgent", event.END_PKG_DOWNLOAD, whenPkgDownloadEnd);

    -- SDK 初始化
    if isIosPlatform() then
        callOCFuncWithClass("AppsFlyerWrap", "afInit");
    end
end

-- 跟踪事件
function trackEvent(eventName, paramName, paramValue)
    paramName  = paramName or "";
    paramValue = paramValue or "";

    trace("AppsFlyerAgent", "trackEvent: eventName = %s, paramName = %s, paramValue = %s",
        eventName, paramName, paramValue);

    if isAndroidPlatform() then
        callJavaFunc("trackAFCustomEvent", {eventName, paramName, paramValue},
            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
    elseif isIosPlatform() then
        callOCFuncWithClass("AppsFlyerWrap", "trackCustomEvent", {
            eventName  = eventName,
            paramName  = paramName,
            paramValue = paramValue,
        });
    end
end

-- 关注进入自动更新场景的事件
function whenAutoPatchEnterScene()
    trackEvent(AFConst.AUTOPATCH_ENTER_SCENE);
end

-- 关注自动更新显示LOGO的事件
function whenAutoPatchShowLogo()
    trackEvent(AFConst.AUTOPATCH_SHOW_LOGO);
end

-- 关注自动更新开始的事件
function whenAutoPatchStart()
    trackEvent(AFConst.AUTOPATCH_START);
end

-- 关注自动更新结果的事件
function whenAutoPatchResult(args)
    local resultCode = args.resultCode;
    local eventName = string.format("%s_%s", AFConst.AUTOPATCH_RESULT, tostring(resultCode));
    trackEvent(eventName);
end

-- 关注自动更新完成的事件
function whenAutoPatchFinish()
    trackEvent(AFConst.AUTOPATCH_FINISH);
end

-- 关注自动更新进入主场景
function whenAutoPatchGotoMain()
    trackEvent(AFConst.AUTOPATCH_GOTO_MAIN);
end

-- 关注SDK第三方账号登录成功的事件
function whenThirdAccountLogined()
    if ME and ME.user then
        -- 已经在游戏中
        return;
    end

    -- 统计SDK登录成功的事件
    trackEvent(AFConst.LOGIN_SDK_LOGIN);

    -- 统计第三方账号登录的事件
    trackEvent(AFConst.LOGIN_SDK_LOGIN .. "_3rdaccount");
end

-- 关注尝试游客登录的事件
function whenTryGuestLogin()
    if ME and ME.user then
        -- 已经在游戏中
        return;
    end

    -- 统计SDK登录成功的事件
    trackEvent(AFConst.LOGIN_SDK_LOGIN);

    -- 统计游客账号登录的事件
    trackEvent(AFConst.LOGIN_SDK_LOGIN .. "_guest");
end

-- 关注登录阶段变化的事件
function whenLoginStageChange(stage)
    local eventName = string.format("%s_%s", AFConst.LOGIN_SERVER_AUTH, tostring(stage));
    trackEvent(eventName);
end

-- 创建新角色的回调
function whenCreateNewChar(args)
    -- 跟踪新玩家首次登录事件
    trackEvent(AFConst.REGISTER);
end

-- 炼金坊强化成功的回调
function whenWorkshopStrengthen(args)
    local id    = args.id;
    local level = args.level;

    if table.indexOf(CARE_WORKSHOP_ITEMS, id) == -1 then
        return;
    end

    if level > 1 then
        return;
    end

    local eventName = string.format("%s_%d", AFConst.ALCHEMIST_BUILDING, id);
    trackEvent(eventName);
end

-- 炼金坊购买装备的回调
function whenWorkshopBuyEquip()
    trackEvent(AFConst.ALCHEMIST_GET_SLIME);
end

-- 玩家开始迷宫的回调
function whenStartDungeon(layer)
    local dungeonId = DungeonM.getDungeonId();
    if dungeonId ~= NEWBIE_DUNGEON_ID or layer ~= 1 then
        return;
    end

    local eventName = string.format("%s_%d.%d", AFConst.ENTER_NEWBIE_DUNGEON, dungeonId, layer);
    local key = getUserKey(eventName);
    if UserDefaultM.getStringForKey(key) == "1" then
        -- 已经跟踪过了
        return;
    end

    UserDefaultM.setStringForKey(key, "1");
    trackEvent(eventName);
end

-- 玩家进入下一层迷宫的回调
function whenNextFloor()
    local dungeonId = DungeonM.getDungeonId();
    if dungeonId ~= NEWBIE_DUNGEON_ID then
        return;
    end

    local layer = DungeonM.currentLayer();
    if layer ~= 2 then
        return;
    end

    local eventName = string.format("%s_%d.%d", AFConst.ENTER_NEWBIE_DUNGEON, dungeonId, layer);
    local key = getUserKey(eventName);
    if UserDefaultM.getStringForKey(key) == "1" then
        -- 已经跟踪过了
        return;
    end

    UserDefaultM.setStringForKey(key, "1");
    trackEvent(eventName);
end

-- 通关迷宫的回调
function whenDungeonPass(args)
    local dungeonId = args.dungeonId;

    -- 跟踪新手关通关的事件
    if dungeonId == NEWBIE_DUNGEON_ID then
        local eventName = string.format("%s_%d", AFConst.CLEAR_NEWBIE_DUNGEON, dungeonId);
        trackEvent(eventName);
        return;
    end

    local parentId = DungeonAreaM.getParentId(dungeonId);

    -- 跟踪解锁无尽模式的事件
    if DungeonAreaM.isAllSubAreaPassed(ME.user, parentId) then
        local eventName = string.format("%s_%d", AFConst.UNLOCK_ENDLESS, parentId);
        trackEvent(eventName);
    end
end

-- 获得冈布奥的回调
function whenGainPet(petId)
    if petId ~= CAPTAIN_PET_ID then
        return;
    end

    -- 跟踪获得舰长冈布奥的事件
    trackEvent(AFConst.GET_SLIME_CAPTAIN);
end

-- 修理天空之城飞艇的回调
function whenSkyRepair()
    -- 跟踪天空之城开启的事件
    trackEvent(AFConst.UNLOCK_SKY_WAR);
end

-- 商城购买的回调
function whenBuyGoods(args)
    local goodsId = args.goodsId;
    if goodsId == SUPERIOR_GOODS_ID then
        -- 跟踪购买月卡的事件
        trackEvent(AFConst.BUY_SIGNBOARD);
    end
end

-- facebook分享成功的回调
function whenFBShareSucc(args)
    local shareType = args.shareType;

    if shareType == SHARE_TYPE_GAME then
        trackEvent(AFConst.FB_SHARE_SETTINGS)
    elseif shareType == SHARE_TYPE_DUNGEON then
        trackEvent(AFConst.FB_SHARE_MAZE)
    elseif shareType == SHARE_TYPE_SLIME then
        trackEvent(AFConst.FB_SHARE_SLIME)
    elseif shareType == SHARE_TYPE_AIRSHIP then
        trackEvent(AFConst.FB_SHARE_AIRSHIP);
    end
end

-- line分享成功的回调
function whenLineShareSucc(args)
    local shareType = args.shareType;

    if shareType == SHARE_TYPE_GAME then
        trackEvent(AFConst.LINE_SHARE_SETTINGS);
    elseif shareType == SHARE_TYPE_DUNGEON then
        trackEvent(AFConst.LINE_SHARE_MAZE);
    elseif shareType == SHARE_TYPE_SLIME then
        trackEvent(AFConst.LINE_SHARE_SLIME);
    elseif shareType == SHARE_TYPE_AIRSHIP then
        trackEvent(AFConst.LINE_SHARE_AIRSHIP);
    end
end

-- 点击论坛的回调
function whenClickForum()
    trackEvent(AFConst.CLICK_FORUM);
end

-- 点击facebook的回调
function whenClickFacebook()
    trackEvent(AFConst.CLICK_FACEBOOK);
end

-- 分包下载开始的回调
function whenPkgDownloadBegin(args)
    local targetPart = args.targetPart;
    if targetPart ~="extra_pack" then
        -- 仅统计独立分包
        return;
    end

    local eventName = AFConst.BEGIN_SUBPACKAGE;
    local key = getUserKey(eventName);
    if UserDefaultM.getStringForKey(key) == "1" then
        -- 已经跟踪过了
        trace("AppsFlyerAgent", "已经统计过extra_pack开始下载事件。");
        return;
    end

    UserDefaultM.setStringForKey(key, "1");
    trackEvent(eventName);
end

-- 分包下载完成的回调
function whenPkgDownloadEnd(args)
    local targetPart = args.targetPart;
    if targetPart ~="extra_pack" then
        -- 仅统计独立分包
        return;
    end

    local eventName = AFConst.END_SUBPACKAGE;
    local key = getUserKey(eventName);
    if UserDefaultM.getStringForKey(key) == "1" then
        -- 已经跟踪过了
        trace("AppsFlyerAgent", "已经统计过extra_pack完成下载事件。");
        return;
    end

    UserDefaultM.setStringForKey(key, "1");
    trackEvent(eventName);
end

-- 获取存储到UserDefault.xml中的key
function getUserKey(eventName)
    if ME.user then
        local rid = ME.user:getRid();
        return string.format("%s_%s", eventName, rid);
    else
        return eventName;
    end
end


